local baoxi = fk.CreateSkill {
  name = "lvshi__baoxi",
  anim_type = "offensive",
  tags = { "Phase" },
}

Fk:loadTranslationTable{
  ["lvshi__baoxi"] = "暴袭",
  [":lvshi__baoxi"] = "阶段技，你可将所有不为此回合获得的手牌当智【火攻】使用，然后你可将所有为此回合获得的手牌当智【水淹七军】使用。若你以此法转化了一张手牌，则你令之造成的伤害+1。",
  ["#lvshi__baoxi"] = "你可选择一名角色，将所有不为此回合获得的手牌当智【火攻】对其使用",
  ["#lvshi__baoxi-drowning"] = "你可以此回合获得的所有手牌当智【水淹七军】使用",
  ["@@baoxi-inhand"] = "暴袭",

  ["$lvshi__baoxi1"] = "群寇蜂起，以军平之。",
  ["$lvshi__baoxi2"] = "所到之处，寇患皆平。",
}

baoxi:addEffect("active", {
  anim_type = "offensive",
  max_phase_use_time = 1,
  card_num = 0,
  target_num = 1,
  prompt = "#lvshi__baoxi",
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and to_select ~= player
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    -- 获取非本回合获得的手牌（火攻）
    local nonObtainedCards = table.filter(player:getCardIds("h"), function(id)
      return not (Fk:getCardById(id):getMark("@@baoxi-inhand") > 0)
    end)
    -- 先使用火攻（非本回合获得的牌）
    if #nonObtainedCards > 0 then
      room:useVirtualCard("lvshi__fire_attack", nonObtainedCards, player, target, baoxi.name)
    end
    -- 获取本回合获得的手牌（水淹七军）
    local obtainedCards = table.filter(player:getCardIds("h"), function(id)
      return Fk:getCardById(id):getMark("@@baoxi-inhand") > 0
    end)
    -- 然后选择是否使用水淹七军（本回合获得的牌）
    if #obtainedCards > 0 then
      local to = room:askToChoosePlayers(player, {
        targets = table.filter(room:getOtherPlayers(player), function (p)
          return player:canUseTo(Fk:cloneCard("lvshi__drowning"), p)
        end),
        max_num = 1,
        min_num = 1,
        skill_name = baoxi.name,
        cancelable = true,
        prompt = "#lvshi__baoxi-drowning",
      })
      if #to > 0 then
        room:useVirtualCard("lvshi__drowning", obtainedCards, player, to[1], baoxi.name)
      end
    end
  end,
})

baoxi:addEffect(fk.CardUsing, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(baoxi.name) and target == player and data.card.skillName == baoxi.name
  end,
  on_use = function(self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.strategy = data.card.trueName.."_scheme"
    if #player.room:getSubcardsByRule(data.card, { Card.Processing }) == 1 then
      data.additionalDamage = (data.additionalDamage or 0) + 1
    end
  end,
})

baoxi:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if not player:hasSkill(baoxi.name) then return end
    for _, move in ipairs(data) do
      return move.to == player and move.toArea == Card.PlayerHand
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Card.PlayerHand then
        for _, info in ipairs(move.moveInfo) do
          room:addCardMark(Fk:getCardById(info.cardId), "@@baoxi-inhand")
        end
      end
    end
  end,
})

baoxi:addAcquireEffect(function (self, player)
  local room = player.room
  -- 获得技能时，标记此回合已经获得的手牌
  local logic = room.logic
  -- 遍历本回合的所有MoveCards事件
  local move_events = logic:getEventsOfScope(GameEvent.MoveCards, 999, function(e)
    return true -- 获取所有MoveCards事件
  end, Player.HistoryTurn)
  for _, move_event in ipairs(move_events) do
    for _, move in ipairs(move_event.data) do
      if move.to == player and move.toArea == Card.PlayerHand then
        for _, info in ipairs(move.moveInfo) do
          -- 判断卡牌当前是否仍在玩家手牌中
          if table.contains(player:getCardIds("h"), info.cardId) then
            room:addCardMark(Fk:getCardById(info.cardId), "@@baoxi-inhand")
          end
        end
      end
    end
  end
end)

baoxi:addEffect(fk.TurnEnd, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(baoxi.name)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    -- 清理所有手牌上的baoxi-inhand标记
    room:clearHandMark(player, "@@baoxi-inhand")
  end,
})

return baoxi